package com.example.a828502.okhttptest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.apache.http.client.HttpClient;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(){
            public void run() {
                try {
                    run22("");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();

    }
    OkHttpClient client = new OkHttpClient();

   public String run22(String url) throws IOException {





           Request request = new Request.Builder()
                .url("http://www.baidu.com")
                .build();


//       Response response = client.newCall(request).execute();
//       if (!response.isSuccessful())
//           throw new IOException("Unexpected code " + response);
//
//       Headers responseHeaders = response.headers();
//      String sss= responseHeaders.toString();
//       for (int i = 0; i < responseHeaders.size(); i++) {
////           System.out.println(responseHeaders.name(i) + :  + responseHeaders.value(i));
//       }
//
//       Toast.makeText(MainActivity.this,response.body().string(),Toast.LENGTH_LONG).show();
////     Log.i("_+_+__+_",);















       //new call
       Call call = client.newCall(request);
//请求加入调度
       call.enqueue(new Callback()
       {
           @Override
           public void onFailure(Request request, IOException e) {

           }

           @Override
           public void onResponse(Response response) throws IOException {
               String htmlStr =  response.body().string();
               Log.i("_+_+_+",htmlStr);
           }
       });


    return "";














//       Response response = client.newCall(request).execute();
//        if (response.isSuccessful()) {
//            String result="";
////
//////            InputStream is  = body.byteStream();
////            Reader reader = body.charStream();
////            try{
////
//////                BufferedReader br=new BufferedReader(new InputStreamReader(is));
////                BufferedReader br=new BufferedReader(reader);
////                String line="";
////                while ((line=br.readLine()).length()>0){
////                    result+=line;
////                }
////                Log.i("_+_+_+",result);
////            }catch (Exception e){
////                e.printStackTrace();
////            }
//            response.getResponseBody();
//            ResponseBody body = response.body();
//            if (null!=body){
//                long ss =  body.contentLength();
//                result=body.toString();
//
//            }
//
//            Log.i("_+_+_+",result);
//            return result;
//        } else {
//            throw new IOException("Unexpected code " + response);
//        }
    }



    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

//    String post(String url, String json) throws IOException {
//        RequestBody body = RequestBody.create(JSON, json);
//        Request request = new Request.Builder()
//                .url(url)
//                .post(body)
//                .build();
//        Response response = client.newCall(request).execute();
//        if(response.isSuccessful()) {
//            return response.body().string();
//        } else {
//            throw new IOException("Unexpected code " + response);
//        }
//    }






//    public void get(){
//        final Request request = new Request.Builder()
//                .url("http://www.weather.com.cn/data/sk/101010100.html")
//                .build();
//        //new call
////        Call call = client.newCall(request);
//////请求加入调度
////        call.enqueue(new Callback()
////        {
////            @Override
////            public void onFailure(Call call, IOException e) {
////
////            }
////
////            @Override
////            public void onResponse(Call call, Response response) throws IOException {
////
////                String htmlStr =  response.body().string();
////            }
////
////
////        });
//
//
//
//    }
}
